package process_demo.wait_and_notify;

/**
 * @author haomin
 * @date 2022/04/25 20:37
 **/
import java.util.concurrent.TimeUnit;

public class Demo3 {
    static Object o = new Object();

    static class MyThread extends Thread {
        @Override
        public void run() {
            synchronized (o) {
                try {
                    o.wait();
                    System.out.println(getName());
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static void main(String[] args) throws InterruptedException {
        for (int i = 0; i < 10; i++) {
            MyThread t = new MyThread();
            t.start();
        }

        // 保证了子线程们先 wait，主线程就先休眠一会儿
        TimeUnit.SECONDS.sleep(5);

        synchronized (o) {
//            o.notify();
            // 唤醒所有o的锁
            o.notifyAll();
        }
    }
}